import TableLyout from "@/components/commonPublic/tableLyout.vue";
import publicForm from "@/components/materialDataManage/fromCom.vue"; //模糊查询
import TableData from "@/components/public/el-table"; //主页面列表
import CustomDialog from "@/components/sampleManage/fieldSampling/index.vue";
import formJs from "./regexDetail.js"; //新增弹框配置项
import actions from "@/actions";

import {
  getSampleNeedWorkJLList, //主页面列表接口
  getfSampletype, //获取样品种类
} from "@/api/request.js";
// import { statusType2 } from "@/utils/config"; //配置项
export default {
  name: "samplesLedger",
  components: {
    TableLyout,
    publicForm, //模糊查询
    TableData, //主页面列表
    CustomDialog, //弹框组件
  },
  data() {
    return {
      name: "samplesLedger", //按钮配置页面代号
      queryCriteriaData: {}, //分页
      searchObj: formJs.searchObj, //搜索条件
      userNature: null, //用户Nature
     
      projectsObj: [], //项目名
      tendersObj: [],
      falgId: null,
      // 列表
      btnObj: formJs.btnObj, //按钮
      table_config: formJs.table_config,
      tableData: [], //列表数据列表
      // checked: true, //查看下级按钮
      loading: false, //列表数据加载loading
      getList: {
        fprojectid: "",
        fCompanyid: "", // 左侧选中组织机构id
        startDates: "", // 开始时间
        endDates: "", // 截至时间
        keyWord: "", // 上方查询条件样品名称
        page: 1, // 当前页码
        pageSize: 15, // 每页显示条数
        userId: "",
        keyType: "", // 样品类型
        modelType: "0",
        timeDates: "",
        fSamplenostr:""
      }, // 列表给后端传参
      // 列表

      // 弹框组件
      currentRow: null, //列表详情
      isShow: false, //是否显示弹框
      statusNum: null, //点击那个按钮
      displaySwitching: 2, //2取样&收样；0取样；1收样
      dialogTitle: null,
      // 弹框组件

      // 左侧机构树

      // 下拉框树
      selectTreeData: [],
      selectCheckData: [],
      selectDefaultProps: {
        children: "children",
        label: "fSampletypename",
      },
      // 下拉框树
      userInfo: {}, //登录用户
    };
  },

  mounted() {
    actions.onGlobalStateChange((state, prev) => {
      var data = prev.currentUser.projects;
      this.userInfo = prev.currentUser
    }, true);
    // this.getfSampletypeFun();
    this.getDate();

    this.table_config.operation.cols = [{
      prop: "fTeststatus",
      type: "primary",
      handleRow: this.handleRow,
    },];

    this.table_config.operation["specialClass"] = function (rowData) {
      var color = null;
      if (rowData == "待检") {
        color = "red !important";
      } else if (rowData == "在检") {
        color = "orange !important";
      } else if (rowData == "已检") {
        color = "green !important";
      } else {
        color = "#409eff !important";
      }
      return color;
    };
    // this.table_config.operation.cols = [
    //   {
    //     label: "查看",
    //     type: "fTeststatus",
    //     handleRow: this.handleRow,
    //   },
    // ];
  },
  methods: {
    
    reset(e) {
      this.getList = this.$options.data.call(this).getList
      this.getList.fprojectid = e
    },
    // 选择项目名称
    selectCompany(e) {
      this.companyName = e.F_ProjectName;
      this.getList.fDepartmentid = e.F_ProjectId;
      this.projectName = "";
      this.getList.fCompanyid = "";
      // 调用施工单位数据
      this.getCompanyIdAndName(e.F_ProjectId);

      this.yj_searchForm(this.getList);
    },
    // 选择施工单位
    selectProject(e) {

      this.projectName = e.F_CompanyName;
      this.getList.fCompanyid = e.F_CompanyId;
      // this.yj_searchForm(this.getList);
    },
    async getTender(e) {
      await this.$http(
        " http://172.18.5.31:8300/IOTManagement/Base/Company/getGXJLBaseCompanyList.do?projectId=" +
        e, {
        headers: {
          "Content-Type": "application/json",
          Authorization: localStorage.getItem("access_token"),
        },
      }
      ).then((res) => {
        if (res.status == 200) {
          var data = res.data.data.GXCompanyList;
          this.tendersObj = [];
          if (data.length == 0) {
            this.$message.warning({
                content: "有其他人员登录此账号！",
                duration: 1000,
            });
            setTimeout(() => {
                window.location.href = "http://172.18.5.201:1080/login";
            }, 1200);
        } else   if (data.length == 1) {
            data.map((item) => {
              this.tendersObj.push({
                fItemName: item.sectionName,
                fItemValue: item.sectionId,
              });
            });
            this.getList.fCompanyid = data[0].sectionId;
          } else {
            this.tendersObj = [{
              fItemName: "所有标段",
              fItemValue: "",
            },];
            data.map((item) => {
              this.tendersObj.push({
                fItemName: item.sectionName,
                fItemValue: item.sectionId,
              });
            });
          }
        }
      });
    },
    selectTender(e) {
      this.getList.stationId = "";
      this.getList.machineId = "";
      this.bhzDataList = [];
      this.bhjDataList = [];

    },
    getBtnName(name, status) {
      this.statusNum = status
      this.yj_edit(status)
    },
    yj_edit(num) {
      this.statusNum = num
      // this.dialogTitle = "闭合处理"
      if (num == 1) { // 导出
        if (this.dataFid != null) {
          // this.editText();
        } else {
          this.$message.warning({
            content: "请先选择需要查看的数据",
            duration: 1000
          })
        }
      }

    },


    // 获取列表数据
    yj_searchForm(data) {
      window.sessionStorage.setItem(
        "GxEmbedded-testDetectionManage_getList",
        JSON.stringify(data)
      );
      var id;
      if (data.fCompanyid != "") {
        id = data.fCompanyid;
      } else {
        id = data.fprojectid;
      }
      var obj = {
        fCompanyid: id, //左侧选中组织机构id
        startDates: data.startDates, //进场开始时间
        endDates: data.endDates, //进场结束时间
        keyWord: data.keyWord, // 上方查询条件样品名称
        page: data.page, //当前页码
        pageSize: data.pageSize, //每页显示条数
        userId: this.userInfo.id, //查询待办或已办时传 列表页查询不传
        // keyType: data.keyType, //上方查询条件材料种类
        // modelType: "0",
        // fSamplenostr:data.fSamplenostr
      };
      this.loading = true;
      var that = this;
      getSampleNeedWorkJLList(obj)
        .then((res) => {
          console.log("列表", res);
          if (res.errcode == 0) {
            that.loading = false;
            that.tableData = res.data.pageInfo.list;
            this.queryCriteriaData = res.data.pageInfo;
          } else {
            this.$message.error("列表数据获取失败", res.msg);
            this.loading = false;
          }
        })
        .catch((error) => {
          this.$message.error("列表数据 接口出错", error.msg);
          this.loading = false;
        });
    },

    // 查看
    yj_viewForm(row, label) {
      if (label == "样品编号") {

        this.currentRow = row;
        this.$router.push({
          path: "/testDetectionManage/samplesLedgerText",

        });
        var data = {
          data: this.currentRow,
          getList: this.getList,
          status: 7,
          displaySwitching: 2,
          changeBtn: true,
        }
        sessionStorage.setItem(
          "GxSampleManage_fieldSampling",
          JSON.stringify(data)
        );

      }
    },
    // 点击当前行
    rowChange(val) {
      this.currentRow = val;
      return val;
    },

    //获取时间
    getDate() {
      if (JSON.parse(sessionStorage.getItem("GxEmbedded-testDetectionManage_getList"))) {
        var dataList = JSON.parse(
          sessionStorage.getItem("GxEmbedded-testDetectionManage_getList")
        );
        if (dataList.fprojectid != "") {
          this.getTender(dataList.fprojectid);
        } else {
          data.fprojectid = this.projectsObj[1].fItemValue;
          this.getTender(dataList.fprojectid);
        }
        if (dataList.startDates == "" || dataList.startDates == undefined || dataList.startDates == null || dataList.endDates == "" || dataList.endDates == undefined || dataList.endDates == null) {
          dataList.startDates = this.common.getDay(-90); // 开始时间
          dataList.endDates = this.common.getDay(0); // 截至时间
        }
        this.getList = {
          fprojectid: dataList.fprojectid,
          fCompanyid: dataList.fCompanyid, // 左侧选中组织机构id
          page: 1, // 当前页码
          pageSize: 15, // 每页显示条数
          userId: this.userInfo.id,
          modelType: "1",
          keyWord: "", // 上方查询条件样品名称

          keyType: "", // 样品类型
          startDates: dataList.startDates, // 开始时间
          endDates: dataList.endDates, // 截至时间
          timeDates: [dataList.startDates, dataList.endDates],
          fSamplenostr:dataList.fSamplenostr
        };
      } else {
        this.getList.startDates = this.common.getDay(-90); // 开始时间
        this.getList.endDates = this.common.getDay(0); // 截至时间
        this.getList.timeDates = [
          this.getList.startDates,
          this.getList.endDates,
        ];
      }
    },

    // 弹框组件
    refreshList() {
      //刷新列表
      this.$nextTick(() => {
        this.yj_searchForm(this.getList);
      });
    },
    closeDialog() {
      this.isShow = false;
    },
    // 弹框组件




    // 筛选条件
    // 下拉框树
    getfSampletypeFun() {
      var data = {
        fChecktypenum: "0", //0自检1外委
        fMaterialtypeid: "", //材料进场材料种类id
      };
      getfSampletype(data)
        .then((res) => {
          if (res.errcode == 0) {
            // this.selectTreeData = this.tranListToTreeData(
            //   res.data.SampletypeList
            // );
            var arr = [{
              children: [],
              fParentId: "0",
              fSamplemodel: "",
              fSampletype: "0",
              fSampletypename: "==请选择=="
            }];
            this.selectTreeData = arr.concat(this.tranListToTreeData(
              res.data.SampletypeList
            ));
            this.searchObj[2].tree.treeData = this.selectTreeData;
            this.searchObj[2].tree.checkData = [
              this.selectTreeData[0].fSampletype,
            ];
          } else {
            this.$message.error("获取样品种类", res.msg);
            this.loadingEditDialog_cl = false;
          }
        })
        .catch((error) => {
          console.log("获取样品种类 接口出错", error.msg);
          this.loadingEditDialog_cl = false;
        });
    },
    tranListToTreeData(data) {
      let result = [];
      if (!Array.isArray(data)) {
        return result;
      }
      // 清除已存在的数据  重新赋值
      data.forEach((item) => {
        delete item.children;
      });

      let map = {};
      data.forEach((item) => {
        map[item.fSampletype] = item;
      });

      data.forEach((item) => {
        let parent = map[item.fParentId];
        if (parent) {
          (parent.children || (parent.children = [])).push(item);
        } else {
          result.push(item);
        }
      });
      return result;
    },
    selectHandleNodeClick(data) {
      if (data.fSampletypename == "==请选择==") {
        this.getList.keyType = ""; //样品种类汉字
      } else {
        this.getList.keyType = data.fSampletypename; //样品种类汉字
      }
    },
    // 下拉框树
    // 时间间隔
    selectTime(e) {
      if (e[0] != "" && e[1] != "") {
        this.getList.startDates = e[0] + " " + "00:00:00"; // 开始时间
        this.getList.endDates = e[1] + " " + "23:59:59"; // 截至时间
      } else {
        this.getList.startDates = ""; // 开始时间
        this.getList.endDates = ""; // 截至时间
      }
      this.getList.timeDates = [e[0], e[1]];
      this.yj_searchForm(this.getList);
    },
    // 筛选条件
  },
};